package com.lx.javasunmmrize.tcp;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Description TODO
 * @Author lx
 * @Date 2021/3/7 15:47
 **/
public class SServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(9002); //创建一个Socket服务器，监听9002端口
        Socket s = ss.accept();//利用Socket服务器的accept()方法获取客户端Socket对象。

        //获取二进制流
        DataInputStream input=new DataInputStream(s.getInputStream());
        byte[] buffer = new byte[20480];
        //消息长度
        int rlength=input.read(buffer, 0, 20480);
        System.out.println();
        System.out.println("接收的消息长度:"+rlength);
        //传输的实际byte[]
        byte[] buffer1 = new byte[rlength];
        for(int i=0;i<buffer1.length;i++){
            buffer1[i]=buffer[i];
        }

        String messageContent1=new String(buffer1,"GBK").toString().trim();
        System.out.println("接收的消息（gbk转码）=============>"+messageContent1);

        String messageContent=new String(buffer,0,rlength).toString().trim();
        System.out.println("接收的消息===========>"+messageContent);
        input.close();
        s.close();
    }
}
